package net.dipper.tankwar.game.ui;

import java.awt.BorderLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JComponent;
import javax.swing.JFrame;

import net.dipper.tankwar.game.control.GameControl;

/*主窗体*/
public class MainWindow extends JFrame implements KeyListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private JComponent currentPane;
	private GameControl control;

	public MainWindow(String title){
		super(title);
		
		addKeyListener(this);
		setLayout(new BorderLayout());
	}
	
	//设置默认显示对象
	public void setPane(JComponent pane){
		if(currentPane != null){
			remove(currentPane);
		}
		
		add(pane, BorderLayout.CENTER);
		validate();
		currentPane = pane;
	}

	public void setGameControl(GameControl control) {
		requestFocus();
		this.control = control;
	}

	@Override
	public void keyPressed(KeyEvent e) {
		if(this.control != null){
			switch(e.getKeyCode()){
			case KeyEvent.VK_LEFT:
				this.control.left();
				break;
				
			case KeyEvent.VK_RIGHT:
				this.control.right();
				break;
				
			case KeyEvent.VK_UP:
				this.control.up();
				break;
				
			case KeyEvent.VK_DOWN:
				this.control.down();
				break;
				
			case KeyEvent.VK_SPACE:
				this.control.shoot();
				break;
			}
		}
	}

	@Override
	public void keyReleased(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void keyTyped(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}
}
